﻿using HardcoreGame.Content.Items.Weapons.Sets.Sleep;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameContent;
using Terraria.GameContent.Animations;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Others
{
    public class DreamItem : ModProjectile
    {
        private static StardewValleyAnimation _stardewAnimation;
        public override void SetStaticDefaults()
        {
            Main.projFrames[Type] = 20;
        }
        public override void SetDefaults()
        {
            Projectile.netImportant = true;
            Projectile.width = 240;
            Projectile.height = 104;
            Projectile.aiStyle = -1;
            Projectile.friendly = true;
            Projectile.penetrate = -1;
            Projectile.tileCollide = false;
            Projectile.ignoreWater = true;
            Projectile.timeLeft *= 5;
        }
        public override void AI()
        {
            AI_192_JuminoAnimation();
        }
        public override bool? CanCutTiles()
        {
            return false;
        }
        public override bool PreDraw(ref Color lightColor)
        {
            SpriteEffects dir = SpriteEffects.None;
            Color projectileColor = Lighting.GetColor((int)(Projectile.position.X + Projectile.width * 0.5) / 16, (int)((Projectile.position.Y + Projectile.height * 0.5) / 16.0));
            Vector2 vector82 = Projectile.position + new Vector2(Projectile.width, Projectile.height) / 2f + Vector2.UnitY * Projectile.gfxOffY - Main.screenPosition;
            Texture2D texture2D4 = TextureAssets.Projectile[Projectile.type].Value;
            Rectangle rectangle22 = texture2D4.Frame(1, Main.projFrames[Projectile.type], 0, Projectile.frame);
            Color color94 = Projectile.GetAlpha(projectileColor);
            AI_192_GetJuminoFall(Projectile, out var timeSinceFall, out var fall);
            vector82.Y -= 4f;
            vector82.Y += fall;
            float amount = Utils.Remap(timeSinceFall, 0f, 10f, 1f, 0f);
            color94 = Color.Lerp(color94, Color.White * Projectile.Opacity, amount);
            Vector2 origin25 = rectangle22.Size() / 2f;
            Main.EntitySpriteDraw(texture2D4, vector82, rectangle22, color94, Projectile.rotation, origin25, Projectile.scale, dir);

            return false;
        }
        private void AI_192_JuminoAnimation()
        {
            float num = 120f;
            float num2 = 420f + num;
            float num3 = num2 - 150f;
            if (Projectile.Opacity > 0f && Projectile.Opacity < 1f)
            {
                for (int i = 0; i < 1; i++)
                {
                    Rectangle r = Utils.CenteredRectangle(Projectile.Center + new Vector2(0f, 20f), new Vector2(32f, 32f));
                    Dust dust = Dust.NewDustDirect(r.TopLeft(), r.Width, r.Height, 263, 0f, 0f, 100, new Color(80, 200, 255), 0.6f);
                    dust.noGravity = true;
                    dust.fadeIn = 0.9f;
                    dust.velocity = new Vector2(0f, -0.3f);
                }
            }
            int num4 = 20;
            int num5 = 4;
            if (Projectile.frame == 1)
            {
                num5 = 60;
            }
            AI_192_GetJuminoFall(Projectile, out var timeSinceFall, out var fall);
            if (timeSinceFall > 0f)
            {
                if (fall < 20f)
                {
                    Projectile.frame = 0;
                }
                else if (++Projectile.frameCounter >= num5)
                {
                    Projectile.frameCounter = 0;
                    if (++Projectile.frame >= num4)
                    {
                        Projectile.frame = num4 - 1;
                    }
                }
            }
            else
            {
                Projectile.frame = 1;
            }
            float num6 = Main.player[Projectile.owner].Center.X - Projectile.Center.X;
            Projectile.direction = (Projectile.spriteDirection = ((num6 >= 0f) ? 1 : (-1)));
            int num7 = 30;
            float num8 = num2 + (float)(num4 * num5) + (float)num7;
            Projectile.Opacity = Utils.Remap(Projectile.ai[0], num3 + 0f, num3 + 20f, 0f, 1f) * Utils.Remap(Projectile.ai[0], num8 - 5f, num8, 1f, 0.5f);
            Projectile.ai[0] += 1f;
            if (Projectile.ai[0] <= 130f && Projectile.ai[0] >= 60f)
            {
                float num9 = Utils.Remap(Projectile.ai[0], 60f, 130f, 0f, 1f);
                if (Main.rand.NextFloat() < num9 * 0.5f)
                {
                    Rectangle r2 = Utils.CenteredRectangle(Projectile.Center + new Vector2(0f, 0f), new Vector2(70f, 70f) * num9);
                    Dust dust2 = Dust.NewDustDirect(r2.TopLeft(), r2.Width, r2.Height, 263, 0f, 0f, 100, new Color(80, 200, 255), 0.6f);
                    dust2.noGravity = true;
                    dust2.fadeIn = 0.9f;
                    dust2.velocity = Main.rand.NextVector2Circular(2f, 2f);
                }
                for (float num10 = 0f; num10 < 1f; num10 += 0.5f)
                {
                    if (Main.rand.NextFloat() < 0.5f)
                    {
                        Dust dust3 = Dust.NewDustPerfect(Projectile.Center, 263, (-Vector2.UnitY).RotatedBy(((0f - num9) * (3f - num9 * 0.6f) + num10 + Main.rand.NextFloatDirection() * 0.05f * (1f - num9)) * ((float)Math.PI * 2f)) * (2f + Main.rand.NextFloat() + Main.rand.NextFloat() * 3f * num9), 30, new Color(80, 200, 255));
                        dust3.noGravity = true;
                        dust3.fadeIn = 1.1f;
                    }
                }
            }
            if (Projectile.ai[0] <= 60f)
            {
                float num11 = Utils.Remap(Projectile.ai[0], 0f, 60f, 0f, 1f);
                for (float num12 = 0f; num12 < 1f; num12 += 0.5f)
                {
                    if (Main.rand.NextFloat() < 0.5f)
                    {
                        Dust dust4 = Dust.NewDustPerfect(Projectile.Center, 263, (-Vector2.UnitY).RotatedBy(((0f - num11) * 3f + num12 + Main.rand.NextFloatDirection() * 0.05f) * ((float)Math.PI * 2f)) * (2f * num11 + Main.rand.NextFloat() * num11), 100, new Color(80, 200, 255), 2f - num11);
                        dust4.noGravity = true;
                        dust4.fadeIn = 1.1f;
                    }
                }
                if (Projectile.ai[0] == 58f)
                {
                    for (float num13 = 0f; num13 < 1f; num13 += 1f / 12f)
                    {
                        Dust dust5 = Dust.NewDustPerfect(Projectile.Center, 263, (-Vector2.UnitY).RotatedBy(((0f - num11) * 2f + num13 + Main.rand.NextFloatDirection() * 0.05f) * ((float)Math.PI * 2f)) * (1f * num11 + Main.rand.NextFloat() * num11 * 2f), 100, new Color(80, 200, 255), 0.8f);
                        dust5.noGravity = true;
                        dust5.fadeIn = 1.2f;
                    }
                }
            }
            if (Projectile.ai[0] >= num8 - 20f)
            {
                float num14 = Utils.Remap(Projectile.ai[0], num8 - 20f, num8, 0.25f, 1f);
                Rectangle r3 = Utils.CenteredRectangle(Projectile.Center + new Vector2(0f, 30f + num14 * -20f), new Vector2(82f, 72f) * num14);
                for (int j = 0; j < 5; j++)
                {
                    Dust dust6 = Dust.NewDustDirect(r3.TopLeft(), r3.Width, r3.Height, 263, 0f, num14 * -4f, 100, new Color(80, 200, 255) * 0.75f, 0.1f);
                    dust6.noGravity = true;
                    dust6.fadeIn = 0.6f + Main.rand.NextFloat() * 0.3f;
                    dust6.velocity = new Vector2(0f, -0.3f);
                }
                for (int k = 0; k < 1; k++)
                {
                    Rectangle r4 = Utils.CenteredRectangle(Projectile.Center + new Vector2(0f, 40f), new Vector2(32f, 32f));
                    Dust dust7 = Dust.NewDustDirect(r4.TopLeft(), r4.Width, r4.Height, 263, 0f, 0f, 100, new Color(80, 200, 255), 0.6f);
                    dust7.noGravity = true;
                    dust7.fadeIn = 0.9f;
                    dust7.velocity = new Vector2(0f, -0.3f);
                }
            }
            if (Main.rand.Next(6) == 0 && Projectile.ai[0] >= 90f && Projectile.ai[0] < num2)
            {
                Rectangle r5 = Utils.CenteredRectangle(Projectile.Center + new Vector2(0f, 0f), new Vector2(104f, 104f));
                Dust dust8 = Dust.NewDustDirect(r5.TopLeft(), r5.Width, r5.Height, 263, 0f, 0f, 100, new Color(80, 200, 255), 0.6f);
                dust8.noGravity = true;
                dust8.fadeIn = 0.9f;
                dust8.velocity = new Vector2(0f, -0.3f);
            }
            if (Projectile.ai[0] >= num8)
            {
                int num15 = Item.NewItem(new EntitySource_Gift(null), Projectile.Center + new Vector2(0f, 12f), Vector2.Zero, ModContent.ItemType<DreamShuriken>());
                Main.item[num15].velocity = new Vector2(0f, 0f);
                Projectile.Kill();
            }
        }
        public static void AI_192_GetJuminoFall(Projectile proj, out float timeSinceFall, out float fall)
        {
            int num = 420;
            timeSinceFall = proj.ai[0] - num;
            if (timeSinceFall < 0f)
            {
                timeSinceFall = 0f;
            }
            int num2 = 24;
            int num3 = -4;
            float num4 = 0.2f;
            fall = num3 * timeSinceFall + num4 * (timeSinceFall * timeSinceFall) / 2f;
            if (fall > num2)
            {
                fall = num2;
            }
        }
    }
}
